package com.pxxy.app_homepage.fragment;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.pxxy.app_homepage.adapter.FragmentHomeRecommendRecyclerAdapter;
import com.pxxy.app_homepage.entity.Information;
import com.pxxy.app_homepage.eventbus.GetTenInfoMessage;
import com.pxxy.lib_common.base.BaseApplication;
import com.pxxy.lib_common.base.BaseFragment;
import com.pxxy.lib_common.entity.User;
import com.pxxy.security512.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;



public class HomeRecommendFragment extends BaseFragment {
    private Banner banner;
    private RecyclerView recyclerView;
    private FragmentHomeRecommendRecyclerAdapter adapter;
    private GlideImageeLoader imageeLoader;
    private List<Integer> images;
    private List<Information> infoList;
    private SmartRefreshLayout refreshLayout;
    @Override
    protected int setLayoutID() {
        return R.layout.homepage_fragment_home_recommend;
    }

    @Override
    protected void initEvent() {
        //设置banner的数据
        banner.setImages(images);
        //设置banneer的加载器
        imageeLoader = new GlideImageeLoader();
        banner.setImageLoader(imageeLoader);
        //启动banner
        banner.start();

        adapter = new FragmentHomeRecommendRecyclerAdapter(getActivity(),infoList);
        //设置点击效果
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(BaseApplication.getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        adapter.setItemListener(new FragmentHomeRecommendRecyclerAdapter.ItemListener() {
            @Override
            public void itemListener(View view, int position, Information info) {
                toastShort(info.getTitle());
            }
        });


        //设置上拉加载和下拉刷新事件事件
        refreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh();
            }
        });

        //设置自动刷新
        refreshLayout.autoRefresh();

    }

    /**
     * 下拉刷新事件
     */
    private void refresh() {

    }

    /**
     * 上拉加载事件
     */
    private void loadMore() {
    }

    /**
     * 订阅事件
     * @param message
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(GetTenInfoMessage message) {

    }

    @Override
    protected void initData() {
        images = new ArrayList<>();
        images.add(R.drawable.a);
        images.add(R.drawable.b);
        images.add(R.drawable.c);
        images.add(R.drawable.f);
        images.add(R.drawable.e);



    }

    @Override
    protected void initView(View view) {
        banner = view.findViewById(R.id.fragment_home_recommend_banner);
        recyclerView = view.findViewById(R.id.fragment_home_recommend_rv);
        refreshLayout = view.findViewById(R.id.fragment_home_recommend_refreshlayout);
        //注册订阅者
        EventBus.getDefault().register(this);
    }



    //轮播图图片加载类
    class  GlideImageeLoader extends ImageLoader{

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //Glide 加载图片简单用法
            Glide.with(context).load(path).into(imageView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
